package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.converter.GoodsConverter;
import com.cskaoyan.wordmemorize.dao.entity.GoodsDO;
import com.cskaoyan.wordmemorize.dao.mapper.GoodsMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteGoodsOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    GoodsConverter goodsConverter;

    // 分页获取商品信息(后台商品管理)
    @Override
    public PageDTO<GoodsDTO> getGoodsPage(PageRequest request) {
        LambdaQueryWrapper<GoodsDO> queryWrapper = new LambdaQueryWrapper<>();
        Page<GoodsDO> page = new Page<>(request.getPageNum(), request.getCount());
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            queryWrapper.eq(GoodsDO::getName, request.getKeyword());
        }
        Page<GoodsDO> goodsDOPage = goodsMapper.selectPage(page, queryWrapper);
        return goodsConverter.goodsPage2PageDTO(goodsDOPage.getRecords(), goodsDOPage.getTotal());

    }

    // 添加商品(后台商品管理)
    @Override
    public void addGoods(GoodsCommand command) {
        GoodsDO goodsDO = goodsConverter.command2GoodsDO(command);
        goodsMapper.insert(goodsDO);
    }

    // 更新商品(后台商品管理)
    @Override
    public void updateGoods(GoodsCommand command) {
        GoodsDO goodsDO = goodsConverter.command2GoodsDO(command);
        goodsMapper.updateById(goodsDO);
    }

    // 删除商品(后台商品管理)
    @Override
    public DeleteGoodsOperateDTO deleteGoods(Long goodsId) {
        GoodsDO goodsDO = goodsMapper.selectById(goodsId);
        goodsMapper.deleteById(goodsId);
        return goodsConverter.goodsDO2DeleteGoodsDTO(goodsDO);

    }

    // 批量删除指定商品(后台商品管理)
    @Override
    public List<DeleteGoodsOperateDTO> deleteGoodsSelected(BatchDeleteCommand command) {
        List<GoodsDO> goodsDOS = goodsMapper.selectByIds(command.getIds());
        goodsMapper.deleteByIds(command.getIds());
        return goodsConverter.goodsDOs2DeleteGoodsDTOs(goodsDOS);

    }

    // 更新商品上下架状态(后台商品管理)
    @Override
    @Transactional
    public void updateGoodsStatus(Long goodsId, Integer status) {
        LambdaUpdateWrapper<GoodsDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(GoodsDO::getId,goodsId);

       updateWrapper.set(GoodsDO::getStatus,status);
        goodsMapper.update(null,updateWrapper);

    }
}
